package com.wntms.gps.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wntms.gps.service.ICarriageContractService;
import com.wntms.gps.pojo.CarriageContract;
import com.wntms.gps.pojo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 运输合同Controller
 * 
 * @author group 4
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/carriageContract")
public class CarriageContractController
{
    @Autowired
    private ICarriageContractService carriageContractService;

    /**
     * 查询运输合同列表
     */

    @GetMapping("/search")
    public Result search(CarriageContract carriageContract,Integer currentPage,Integer pageSize)
    {
        PageHelper.startPage(currentPage,pageSize);

        List<CarriageContract> list = carriageContractService.selectCarriageContractList(carriageContract);

        PageInfo<CarriageContract> pageInfo = new PageInfo<>(list);

        return Result.success(pageInfo,"查询成功");
    }

    @GetMapping("/list")
    public Result list(Integer currentPage,Integer pageSize)
    {

        PageHelper.startPage(currentPage,pageSize);;

        List<CarriageContract> all = carriageContractService.findAll();

        PageInfo<CarriageContract> pageInfo = new PageInfo<>(all);

//        PageInfo<CarriageContract> pageInfo = PageHelper.startPage(currentPage,pageSize).doSelectPageInfo(
//                ()->carriageContractService.findAll());

//        System.out.println(pageInfo);

        Result result = Result.success(pageInfo,"查询成功");

//        System.out.println(result);

        return result;
    }

    /**
     * 获取运输合同详细信息
     */

    public Result getInfo(@PathVariable("cid") String cid)
    {
        return Result.success(carriageContractService.selectCarriageContractByCid(cid),"查询成功");
    }

    /**
     * 新增运输合同
     */

    @PostMapping
    public Result add(@RequestBody CarriageContract carriageContract)
    {
        //TODO 新增运输合同与CustomerIn,CustomerInformation
        int i = carriageContractService.insertCarriageContract(carriageContract);

        if(i!=0){
            return Result.success(i,"插入成功");
        }else{
            return Result.error("插入失败");
        }

    }

    /**
     * 修改运输合同
     */

    @PutMapping
    public Result edit(@RequestBody CarriageContract carriageContract)
    {
        int i = carriageContractService.updateCarriageContract(carriageContract);
        if(i!=0){
            return Result.success(i,"操作成功");
        }else{
            return Result.error("操作失败");
        }
    }

    /**
     * 删除运输合同
     */

	@DeleteMapping("/{cids}")
    public Result remove(@PathVariable String[] cids)
    {
        int i = carriageContractService.deleteCarriageContractByCids(cids);
        if(i!=0){
            return Result.success(i,"删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
}
